package com.live.dengdengtv.activity.setting;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.live.dengdengtv.CustomTitleActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.utils.EmptyViewUtil;
import com.live.dengdengtv.utils.Notice;
import com.tencent.TIMFriendResult;
import com.tencent.TIMFriendshipManager;
import com.tencent.TIMUserProfile;
import com.tencent.TIMValueCallBack;

import java.util.ArrayList;
import java.util.List;

public class BlacklistActivity extends CustomTitleActivity implements AdapterView.OnItemClickListener {

    private static final String TAG = "BlacklistActivity";
    private ListView listView;

    private List<TIMUserProfile> ListData = new ArrayList<TIMUserProfile>();
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blacklist,R.layout.title_custom_red);
        setDefaultTitle("黑名单");

        listView = (ListView) findViewById(R.id.listView);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

        View emptyView = EmptyViewUtil.getEmptyView(getApplicationContext(),"这里什么都没有哦~");
        ((ViewGroup)listView.getParent()).addView(emptyView);
        listView.setEmptyView(emptyView);
        getBlackList();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
        Notice.alert(context, "移除黑名单?", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                removeBlackList(position);
            }
        });
    }

    private void removeBlackList(final int position){

        List<String> blackList = new ArrayList<String>();
        blackList.add(ListData.get(position).getIdentifier());
        TIMFriendshipManager.getInstance().delBlackList(blackList, new TIMValueCallBack<List<TIMFriendResult>>() {
            @Override
            public void onError(int code, String desc) {
                Log.e(TAG, "getBlackList failed: " + code + " desc" + desc);
            }

            @Override
            public void onSuccess(List<TIMFriendResult> timFriendResults) {
                Toast.makeText(context,"移除黑名单成功！",Toast.LENGTH_SHORT).show();
                ListData.remove(position);
                adapter.notifyDataSetChanged();
            }
        });
    }
    private void getBlackList(){
        TIMFriendshipManager.getInstance().getBlackList(new TIMValueCallBack<List<String>>() {
            @Override
            public void onError(int code, String desc) {
                Log.e(TAG, "getBlackList failed: " + code + " desc" + desc);
            }

            @Override
            public void onSuccess(List<String> userIds) {
                getUserInfo(userIds);
            }
        });
    }

    private void getUserInfo(List<String> userIds){

        TIMFriendshipManager.getInstance().getUsersProfile(userIds, new TIMValueCallBack<List<TIMUserProfile>>() {
            @Override
            public void onError(int code, String desc) {
                Log.e(TAG, "getBlackListUserInfo failed: " + code + " desc" + desc);
            }
            @Override
            public void onSuccess(List<TIMUserProfile> timUserProfilesList) {
                if(timUserProfilesList.size() == 0){
                    Toast.makeText(context,"没有黑名单！",Toast.LENGTH_SHORT).show();
                }
                ListData = timUserProfilesList;
                adapter.notifyDataSetChanged();
            }
        });
    }



    private class MyAdapter extends  BaseAdapter{
        @Override
        public int getCount() {
            return ListData.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView != null) {
                holder = (ViewHolder)convertView.getTag();
            } else {
                convertView = View.inflate(context, R.layout.item_blacklist, null);

                holder = new ViewHolder();
                holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
                holder.msg_tv = (TextView) convertView.findViewById(R.id.msg_tv);
                holder.head_iv = (ImageView) convertView.findViewById(R.id.head_iv);
                holder.sex_iv = (ImageView) convertView.findViewById(R.id.sex_iv);
                holder.level_iv = (ImageView) convertView.findViewById(R.id.level_iv);

                convertView.setTag(holder);
            }

            TIMUserProfile userProfile = ListData.get(position);

            String id = userProfile.getIdentifier();
            String nickName = userProfile.getNickName();
            String selfSignature = userProfile.getSelfSignature();
            String faceUrl = userProfile.getFaceUrl();
            String sex = userProfile.getGender().getValue()+"";
//            String level = userProfile.get);

            holder.name_tv.setText(TextUtils.isEmpty(nickName)? id: nickName);
            holder.msg_tv.setText(selfSignature);
            if(!TextUtils.isEmpty(faceUrl)){
                Glide.with(context).load(faceUrl).into(holder.head_iv);
            }
            return convertView;
        }
    }

    private static class ViewHolder{
        TextView name_tv;
        TextView msg_tv;

        ImageView head_iv;
        ImageView sex_iv;
        ImageView level_iv;
    }
}
